<?
/*
* //Example of usage:
//config:
$searchFields = array("username", "userID"); // array (required) The fields where the search field should search
$condition = "alive='Y'"                     // string (optional) Extra Condition if needed
$order = "username"                          // string (optional)
$groupBy = "userID"
// variables setted
.
$condition;      // string, tells the condition in SQL Query for $searchFields fields
$groupBy		 // string, tells the mysql statement GROUP BY.
$position;       // int, tells the current row position
$numTotalRows;   // int, tells the total number of rows (without pagination)
$htmlSearchForm; // string, holds the HTML search Form
$showFromTo;     // string, holds a string telling the current position
$urlVars;        // string, needed for list pagination
$newOrderType    // string, its prepare the order type for ordering by particular a column
*/

// SEARCH condition and HTML
$condition = $condition? $condition : "1";
if(count($searchFields) > 0)
{
	if($_GET['search'])
	{
		$condition .= " AND (". implode(" LIKE '%". mysql_escape_string(urldecode($_GET['search'])) ."%' OR ", $searchFields) ." LIKE '%". mysql_escape_string(urldecode($_GET['search'])) ."%')";
	}

	foreach($_GET as $queryField => $queryVariable)
	{
		if(($queryField != "search") && !is_array($queryVariable) && ($queryField != "action"))
			$hiddenFields .= '<input type="hidden" name="'. urlencode($queryField) .'" value="'. urlencode($queryVariable) .'">';
	}
	$htmlSearchForm = '
	<form name="form1" action="'.$_SERVER['PHP_SELF'].'" method="get">
	  '.$hiddenFields.'
	  <div id="SearchFormBox">
		<table border="0" cellpadding="0" cellspacing="0" class="Form">
		  <tr>
			<td>'. $lang->translate("search") .':&nbsp;&nbsp;<input type="hidden" name="mod" value="submit"><br>
			</td>
			<td align="right"><input name="search" type="text" class="Form" id="search2" value="'. htmlspecialchars(urldecode($_GET['search'])) .'" size="40" maxlength="40"></td>
			<td><span class="button"><input type="submit" name="buscar" value="'. $lang->translate("search") .'" /></span></td>
		  </tr>
		</table>
	  </div>
	</form>
	';
}

// FOR ORDERING
if (isset($_GET['order']) && $_GET['order'] != "")
{
    $order = "`".$_GET['order']."`";
    switch($_GET['order_type'])
    {
        case "DESC": $order .= " DESC"; break;
        case "ASC":  $order .= " ASC"; break;
    }
}
$order = $order? " ORDER BY ". $order : ($indexIDPrefix.$indexID ? " ORDER BY ".$indexIDPrefix.$indexID." DESC" : "" );


// URL VARS FOR THE PAGEING FUNCTION
$urlVars = "order=". urlencode($_GET['order']) ."&order_type=". urlencode($_GET['order_type']) ."&search=". urlencode($_GET['search']);


// URL FOR COLUMN ORDERING
$columnUrl = "search=". urlencode($_GET['search'] ) ."&amp;".(urldecode($_GET['order_type'])=="DESC"? "order_type=ASC" : "order_type=DESC") ."&amp;order=";

/*
***** FOR COUNTING *****
* A SELECT statement may include a LIMIT clause to restrict the number of rows the server
* returns to the client. In some cases, it is desirable to know how many rows the statement*
* would have returned without the LIMIT, but without running the statement again. To obtain
* this row count, include a SQL_CALC_FOUND_ROWS  option in the SELECT  statement, and then
* invoke FOUND_ROWS() afterward:
*
* mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
*     -> WHERE id > 100 LIMIT 10;
* mysql> SELECT FOUND_ROWS();
*
* http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
*/
if (isset($countWithFoundRows)) $sql = "SELECT FOUND_ROWS()";
else if ($myQuery2Count) $sql = $myQuery2Count;
else $sql = "SELECT DISTINCT ". $indexIDPrefix.$indexID ." FROM ". $table ." WHERE ". $condition.($groupBy ? " GROUP BY ".$groupBy : "");
$result  = mysql_query($sql, $dbLink);
echo mysql_error();
$numTotalRows = mysql_num_rows($result);
mysql_free_result($result);


// PAGINATION VARIABLES
$position = isset($_GET['position'])? intval($_GET['position']) : 0;
$max = $position + NUM_MAX_LISTED_ROWS > $numTotalRows? $numTotalRows : $position + NUM_MAX_LISTED_ROWS;
$showFromTo = isset($_GET['search'])? " (Resultados de ". ( string ) ($position+1) ." a ". ( string )$max .") " : "";

?>